package cn.zm.trip.web.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.zm.trip.web.dao.ForumDao;
import cn.zm.trip.web.dao.HotelDao;
import cn.zm.trip.web.domain.*;
import cn.zm.trip.web.service.FoodService;
import cn.zm.trip.web.service.HotelFacilityService;
import cn.zm.trip.web.service.ViewPointService;
import cn.zm.trip.web.service.WordsService;
import cn.zm.trip.web.utils.BeanCopyUtils;
import cn.zm.trip.web.vo.FoodVo;
import cn.zm.trip.web.vo.HotelVO;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "article")
public class ArticleController {
	//声明用于存放留言回复信息的List集合
	private List<Words> lw_list;
	private List<Reply> lr_list;

	@Autowired
	ViewPointService viewPointService;

	@Autowired
	WordsService wordsService;

	@Autowired
	ForumDao forumDao;
	@Autowired
	HotelDao hotelDao;
	@Autowired
	private HotelFacilityService hotelFacilityService;
	@Resource
	private  FoodService foodService;
	/**
	 * 保存景点留言信息
	 */
	@RequestMapping(value="/saveWords")
	public String saveWords(Words words){
		if(words != null){
			String r_id = words.getLw_for_article_id();
			wordsService.saveWords(words);
			return "redirect:toArticleView?r_id="+r_id;
		}else{
			return null;
		}
	}

	/**
	 * 保存回复信息
	 */
	@RequestMapping(value="/saveReply")
	public String saveReply(Reply reply){
		if(reply != null){
			wordsService.saveReply(reply);
			String r_id = reply.getLr_for_article_id();
			return "redirect:toArticleView?r_id="+r_id;
		}else{
			return null;
		}
	}

	/**
	 * 跳转到查看文章内容页面
	 */
	@RequestMapping(value="/toArticleView")
	public String toArticleView(@RequestParam("r_id") int r_id, Model model){
		//封装留言信息
		List<Words> lw_list = wordsService.findByWords();
		model.addAttribute("lw_list",lw_list);

		//封装回复信息
		List<Reply> lr_list = wordsService.findByReply();
		model.addAttribute("lr_list",lr_list);

		//留言统计接口
		List<WordStatisticsEntity> wordStatisticsEntities = wordsService.listWordStaticsByWordQuery(r_id, null,null);
		if (CollUtil.isNotEmpty(wordStatisticsEntities)) {
			int sum = wordStatisticsEntities.stream().mapToInt(WordStatisticsEntity::getQuantity).sum();

			for (WordStatisticsEntity statisticsEntity : wordStatisticsEntities) {
				Integer quantity = statisticsEntity.getQuantity();
				double radio = (quantity * 1.0) / sum;
				String proportion = (int) ((radio) * 100) + "%";
				statisticsEntity.setScoreTextByScore();
				statisticsEntity.setScoreRatioText(proportion);
			}
			model.addAttribute("wordStatics",wordStatisticsEntities);
		}

		ViewPoint viewPoint = viewPointService.selectByPrimaryKey(r_id);
		viewPoint.setPictureList(JSON.parseArray(viewPoint.getPictureUrls(),String.class));
		model.addAttribute("viewPoint", viewPoint);
		return "proscenium/viewpoint/content";
	}


	/**
	 * 保存论坛回复信息
	 */
	@RequestMapping(value="/saveForumReply")
	public String saveForumReply(Reply reply){
		if(reply != null){
			wordsService.saveReply(reply);
			String tpFid = reply.getLr_forumId();
			return "redirect:toForumArticleView?tpFid=" + tpFid;
		}else{
			return null;
		}
	}

	/**
	 * 保存论坛留言信息
	 */
	@RequestMapping(value="/saveforumWords")
	public String saveForumWords(Words words){
		if(words != null){
			String forumId = words.getLw_forumId();//获取什么类型的ID
			wordsService.saveWords(words);
			return "redirect:toForumArticleView?tpFid=" + forumId;
		}else{
			return null;
		}
	}

	/**
	 * 跳转到论坛
	 */

	@RequestMapping(value="/toForumArticleView")
	public String toForumArticleView(@RequestParam int tpFid, Model model){
		//封装留言信息
		lw_list = wordsService.findByWords();
		model.addAttribute("lw_list",lw_list);

		//封装回复信息
		lr_list = wordsService.findByReply();
		model.addAttribute("lr_list",lr_list);

		//查询文章信息
		Forum forum = forumDao.selectByPrimaryKey(tpFid);

		System.out.println("查询到当前文章的ID值：" + forum.getTpFid());

		if (forum != null) {
			model.addAttribute("forum", forum);
			return "proscenium/forum/content";
		} else {
			return null;
		}
	}


	/**
	 * 保存论坛回复信息
	 */
	@RequestMapping(value="/saveHotelReply")
	public String saveHotelReply(Reply reply){
		if(reply != null){
			wordsService.saveReply(reply);
			String hid = reply.getLr_hotel_id();
			return "redirect:toHotelArticleView?hid=" + hid;
		}else{
			return null;
		}
	}

	/**
	 * 保存论坛留言信息
	 */
	@RequestMapping(value="/saveHotelWords")
	public String saveHotelWords(Words words){
		if(words != null){
			String hid = words.getLw_hotel_id();
			wordsService.saveWords(words);
			return "redirect:toHotelArticleView?hid=" + hid;
		}else{
			return null;
		}
	}
	/**
	 * 保存论坛留言信息
	 */
	@RequestMapping(value="/saveFoodWords")
	public String saveFoodWords(Words words){
		if(words != null){
			String hid = words.getLw_food_id();
			wordsService.saveWords(words);
			return "redirect:toFoodArticleView?hid=" + hid;
		}else{
			return null;
		}
	}

	/**
	 * 跳转到论坛
	 */

	@RequestMapping(value="/toHotelArticleView")
	public String toHotelArticleView(@RequestParam int hid, Model model){
		//封装留言信息
		lw_list = wordsService.findByWords();
		model.addAttribute("lw_list",lw_list);

		//封装回复信息
		lr_list = wordsService.findByReply();
		model.addAttribute("lr_list",lr_list);

		//查询文章信息
		Hotel hotel = hotelDao.selectByPrimaryKey(hid);

		System.out.println("查询到当前文章的ID值：" + hotel.getHid());
		//将字符串数组转成字符串
		HotelFacility hotelFacility = hotelFacilityService
				.getOne(new LambdaQueryWrapper<HotelFacility>().eq(HotelFacility::getHotelId, hotel.getHid()));
		model.addAttribute("hotel", HotelVO.buildHotelVO(hotel,hotelFacility));
		return "proscenium/hotel/content";
	}

	/**
	 * 跳转到美食
	 */

	@RequestMapping(value="/toFoodArticleView")
	public String toFoodArticleView(@RequestParam int hid, Model model){
		//封装留言信息
		lw_list = wordsService.findByWords();
		model.addAttribute("lw_list",lw_list);

		//封装回复信息
		lr_list = wordsService.findByReply();
		model.addAttribute("lr_list",lr_list);

// 从食品服务获取食品
		Food food = foodService.getById(hid);
		// 将食品列表添加到模型中，以便在前端页面中访问
		FoodVo foodVo = BeanCopyUtils.copyBean(food, FoodVo.class);
		if (CharSequenceUtil.isNotEmpty(foodVo.getPictureUrls())){
			foodVo.setPictureLists(JSONUtil.toBean(food.getPictureUrls(), new TypeReference<List<String>>(){},true));
		}
		model.addAttribute("food", foodVo);
		return "proscenium/food/content";
	}

}
